package com.heima.admin.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.service.AdChannelService;
import com.heima.model.admin.dtos.ChannelDTO;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {
  @Override
  public ResponseResult findByNameAndPage(ChannelDTO dto) {
    if(dto==null){
      return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
    }
    dto.checkParam();
    //按名称模糊分页查询
    LambdaQueryWrapper<AdChannel> lambdaQueryWrapper = Wrappers.lambdaQuery();
    if(StringUtils.isNotBlank(dto.getName())){
      lambdaQueryWrapper.like(AdChannel::getName,dto.getName());
    }
    // 添加排序
    lambdaQueryWrapper.orderByAsc(AdChannel::getOrd);
    Page page = new Page(dto.getPage(),dto.getSize());
    IPage result = page(page, lambdaQueryWrapper);
    //结果封装
    ResponseResult responseResult = new PageResponseResult(dto.getPage(),dto.getSize(),result.getTotal(),result.getRecords());
    return responseResult;
  }
  //新增
  @Override
  public ResponseResult insertAdchannel(AdChannel adChannel) {
    if (adChannel==null || StringUtils.isBlank(adChannel.getName())){
      return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
    }
    int count = this.count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName, adChannel.getName()));
    if (count>0){
      return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
    }
    if (adChannel.getName().length()>10){
      return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
    }
    adChannel.setCreatedTime(new Date());
    save(adChannel);
    return ResponseResult.okResult();
  }

  @Override
  public ResponseResult updateAdchannel(AdChannel adChannel) {
    if (adChannel==null||adChannel.getId()==null){
      return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
    }
    AdChannel yadChannel = getById(adChannel.getId());
    if (yadChannel==null){
      return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
    }
    if (StringUtils.isNotBlank(adChannel.getName())&&!adChannel.getName().equals(yadChannel.getName())){
      int count = this.count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName, adChannel.getName()));
      if (count>0){
        return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
      }
      if (adChannel.getName().length()>10){
        return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
      }
    }
    updateById(adChannel);
    return ResponseResult.okResult();
  }

  @Override
  public ResponseResult deleteAdchannel(Integer id) {
    if (id==null){
      return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
    }
    AdChannel yadChannel = getById(id);
    if (yadChannel==null){
      return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
    }
    if (yadChannel.getStatus()){
      return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_ALLOW);
    }
    removeById(id);
    return ResponseResult.okResult();
  }
}